﻿Imports Equipment.Core
Imports Equipment.DataAccess

Public Class UserInfo
    Implements IUserInfo

#Region "Constructor"
    Public Sub New()
        Me.SiteID = 0
        Me.UserID = 0
        Me.FullName = "Unknown"
        Me.IsSuperUser = False
        Me.IsSiteAdmin = False
        Me.TimezoneID = ""
        Me._timezoneAbbr = ""
        Me._RequiredAuthenticationForWorkOrder = Sys.RequiredAuthenticationForWorkOrder
    End Sub

    Public Sub New(ByVal usr As User)
        Me.UserID = usr.ID
        Me.SiteID = usr.SiteID
        Me.FullName = usr.FullName
        Me.IsSuperUser = False
        Me.IsSiteAdmin = False
        Me.Email = usr.Email
        Me.TimezoneID = usr.Timezone
        Me._RequiredAuthenticationForWorkOrder = Sys.RequiredAuthenticationForWorkOrder
        LoadUserPageAccessInfo(usr)
        LoadUserRoleInfo(usr)
        LoadUserGroupInfo(usr)
    End Sub
#End Region

#Region "Properties"

    Private _pageAccessList As New Dictionary(Of PageType, String)
    Private _groupIDList As New List(Of Integer)
    Private _roleIDList As New List(Of Integer)

    Private _userID As Integer
    Public Property UserID As Integer Implements IUserInfo.UserID
        Get
            Return _userID
        End Get
        Set(ByVal value As Integer)
            _userID = value
        End Set
    End Property

    Private _siteID As Integer
    Public Property SiteID As Integer Implements IUserInfo.SiteID
        Get
            Return _siteID
        End Get
        Set(ByVal value As Integer)
            _siteID = value
        End Set
    End Property

    Private _userFullName As String
    Public Property FullName As String
        Get
            Return _userFullName
        End Get
        Set(ByVal value As String)
            _userFullName = value
        End Set
    End Property

    Private _userEmail As String
    Public Property Email As String
        Get
            Return _userEmail
        End Get
        Set(ByVal value As String)
            _userEmail = value
        End Set
    End Property

    Private _isSuperUser As Boolean
    Public Property IsSuperUser As Boolean Implements IUserInfo.IsSuperUser
        Get
            Return _isSuperUser
        End Get
        Set(ByVal value As Boolean)
            _isSuperUser = value
        End Set
    End Property

    Private _isSiteAdmin As Boolean
    Public Property IsSiteAdmin As Boolean Implements IUserInfo.IsSiteAdmin
        Get
            Return _isSiteAdmin
        End Get
        Set(ByVal value As Boolean)
            _isSiteAdmin = value
        End Set
    End Property

    Private _timezoneID As String
    Public Property TimezoneID As String Implements IUserInfo.TimeZoneID
        Get
            Return _timezoneID
        End Get
        Set(ByVal value As String)
            _timezoneID = value
            If Not String.IsNullOrEmpty(value) Then
                Me._timezoneAbbr = TimeZoneManager.GetAbbreviationByID(value)
                Me._timezoneOffset = TimeZoneManager.GetOffsetByID(value)
            End If
        End Set
    End Property

    Private _timezoneAbbr As String
    Public ReadOnly Property TimezoneAbbr As String Implements IUserInfo.TimeZoneAbbr
        Get
            Return _timezoneAbbr
        End Get
    End Property

    Private _timezoneOffset As TimeSpan
    Public ReadOnly Property TimezoneOffset As TimeSpan Implements IUserInfo.TimeZoneOffset
        Get
            Return _timezoneOffset
        End Get
    End Property

    Private _RequiredAuthenticationForWorkOrder As Boolean
    Public Property RequiredAuthenticationForWorkOrder As Boolean Implements IUserInfo.RequiredAuthenticationForWorkOrder
        Get
            Return _RequiredAuthenticationForWorkOrder
        End Get
        Set(ByVal value As Boolean)
            _RequiredAuthenticationForWorkOrder = value
        End Set
    End Property


    Public Property GroupIDs As List(Of Integer) Implements IUserInfo.GroupIDs
        Get
            Return _groupIDList
        End Get
        Set(ByVal value As List(Of Integer))
            _groupIDList = value
        End Set
    End Property

    Public Property RoleIDs As List(Of Integer) Implements IUserInfo.RoleIDs
        Get
            Return _roleIDList
        End Get
        Set(ByVal value As List(Of Integer))
            _roleIDList = value
        End Set
    End Property

    Public ReadOnly Property IsAuthenticated As Boolean
        Get
            Return Me.UserID > 0
        End Get
    End Property

    Public Function HasPermissionFor(ByVal access As PageType) As Boolean
        If _pageAccessList.ContainsKey(access) Or IsSuperUser Then
            Return True
        End If
        Return False
    End Function

#End Region

#Region "Private Section"
    Private Sub LoadUserPageAccessInfo(ByVal usr As User)
        Dim hasAccessForAllPages As Boolean = False
        Me.IsSuperUser = usr.Roles.AsEnumerable().Count(Function(r) r.IsSuperRole = True) > 0
        Me.IsSiteAdmin = usr.Roles.AsEnumerable().Count(Function(r) r.IsSiteAdmin = True) > 0
        hasAccessForAllPages = Me.IsSuperUser Or Me.IsSiteAdmin

        For Each currRole In usr.Roles
            For Each access In PageAccess.RetrieveAllByStatusString(currRole.PageAccess)
                If (access.Grant Or hasAccessForAllPages) AndAlso Not _pageAccessList.ContainsKey(access.Type) Then
                    _pageAccessList.Add(access.Type, access.Description)
                End If
            Next
        Next
    End Sub

    Private Sub LoadUserRoleInfo(ByVal usr As User)
        For Each currRole In usr.Roles
            _roleIDList.Add(currRole.ID)
        Next
    End Sub

    Private Sub LoadUserGroupInfo(ByVal usr As User)
        For Each currGroup In usr.Groups
            _groupIDList.Add(currGroup.ID)
        Next
    End Sub
#End Region
End Class
